import 'package:kkhousekeep/net/base_connect.dart';
import 'package:kkhousekeep/tools/dialog_util.dart';

import '../models/home_add_visitor_record_entity.dart';
import '../models/home_visitor_record_detail_entity.dart';
import '../models/home_visitor_record_entity.dart';
import '../models/login_send_code.dart';
import 'api.dart';

class HomeVisitorConnect extends BaseConnect {
  Future<HomeVisitorRecordEntity?> getRecordList() async {
    var response = await post(Api.getVisitorRecordList, {});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(HomeVisitorRecordEntity.fromJson(response.body));
  }

  Future<HomeAddVisitorRecordEntity?> addRecord(
      {required String visitor,
      required String phoneNumber,
      required int visitorNum,
      required String visitStartTime,
      required String visitEndTime,
      required String licensePlate,
      required String visitReason}) async {
    var response = await post(Api.addVisitorRecord, {
      "visitor": visitor,
      "phoneNumber": phoneNumber,
      "visitorNum": visitorNum,
      "visitStartTime": visitStartTime,
      "visitEndTime": visitEndTime,
      "licensePlate": licensePlate,
      "visitReason": visitReason,
    });
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(
      HomeAddVisitorRecordEntity.fromJson(response.body),
    );
  }

  Future<HomeVisitorRecordDetailEntity?> getVisitorDetail(
      {required int visitorId}) async {
    var response = await post(Api.getVisitorDetail, {"id": visitorId});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(
      HomeVisitorRecordDetailEntity.fromJson(response.body),
    );
  }

  Future<SendCodeEntity?> sendCodeToVisitor(int visitorId) async {
    var response = await post(Api.sendCodeToVisitor, {"id": visitorId});
    if (response.body == null) {
      DialogUtil.showNetworkError();
      return Future.value();
    }
    return Future.value(SendCodeEntity.fromJson(response.body));
  }
}
